<script>
	var tableoption={
			toolbar: '#toolbar',//工具按钮用哪个容器
			clickToSelect: true,// 单击行即可以选中
			singleSelect: true, //设置true禁止多选
			sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
            showPaginationSwitch: true,
			showExport: true,
			exportDataType: 'all',
			exportTypes: ['doc', 'excel'], //导出文件类型
			showToggle: true,  //是否显示视图切换按钮
			showRefresh: true,// 是否显示刷新按钮
			showColumns: true,//是否显示所有的列
			search: true, // 开启搜索功能
			pagination: true,// 开启分页功能
			sortName: "sysactionid", // 设置默认排序为 name
			columns: [{
				field: "checked",
				checkbox: true
				}, {
				field: "isdefault",
				title: "是否默认",
				align: "center",
				valign: "middle"
				}, {
				field: "actionid",
				title: "权限编号",
				align: "center",
				valign: "middle"
				}, {
				field: "sysactionname",
				title: "权限名",
				align: "center",
				valign: "middle"
				}, {
				field: "sysactionid",
				title: "系统权限编号",
				visible: false,
				align: "center",
				valign: "middle"
				}, {
				field: "systemid",
				title: "系统编号",
				visible: false,
				align: "center",
				valign: "middle"
				}, {
				field: "systemname",
				title: "系统名称",
				align: "center",
				valign: "middle"
				}, {
				field: "params",
				title: "参数",
				align: "center",
				valign: "middle"
				}, {
				field: "upname",
				title: "上级权限名称",
				align: "center",
				valign: "middle"
				}, {
				field: "upid",
				title: "上级权限",
				visible: false,
				align: "center",
				valign: "middle"
				}, {
				field: "isused",
				title: "使用",
				align: "center",
				formatter : function (value, row, index) { switch(value) {case 1 :return "是"; default : return "否"}},
				valign: "middle"
			}],
			onDblClickRow: function (row) {
				if (actions.includes("109")) {
					row.checked=true;
					expendp($.fn.zTree.getZTreeObj("Tree_all"),$.fn.zTree.getZTreeObj("Tree_all").getNodeByParam("sysactionid",row.sysactionid));
					EditTable(row);
				}
			}
		}

	$(function() {
		checkaction(new Array("108","109","111","112"));
		$("#QueryTable").bootstrapTable(tableoption);
		$("#QueryTable").bootstrapTable('showLoading');
		ajaxarr([{
				url:"sysinfo/systeminfo_query",
				title:"获取系统信息失败！",
				data:{token:token},
				func: function(data) {
					var systemid="<option value=''>请选择系统</option>";
					if (data.info.total>0) {
						for (i=0,j=data.info.rows.length;i<j;i++) { systemid+="<option value='"+data.info.rows[i].systemid+"'>"+data.info.rows[i].systemname+"</option>";}
					}
					$("#txt_systemid").html(systemid);
				$(".selectpicker").selectpicker("refresh");
				}
		}]);
		$('#upModal').on('hidden.bs.modal', function(e) { $("body").addClass("modal-open");  });
		$("#myModal").on("shown.bs.modal",function(e){ if ($("#txt_sysactionid").attr("disabled")) { $("#txt_upid").focus() } else {$("#txt_sysactionid").focus()}; });
		QueryTable();
		$(".selectpicker").selectpicker("refresh");
	});
	
	function AddTable() {//新增
		$("#txt_isdefault").val("");
		$("#txt_actionid").val("");
		$("#txt_sysactionname").val("");
		$("#txt_sysactionid").val("");
		$("#txt_sysactionid").attr("disabled",false);
		$("#txt_systemid").selectpicker("val","");
		$("#txt_params").val("");
		$("#txt_upid").val("");$("#txt_upid").data("value","");
        $("#myModalLabel").html("新增");
        $("#myModal").modal();
	};
	
	function EditTable(row) {
		if (row==null) {
			if ($("#QueryTable").bootstrapTable('getSelections').length == 0) {
				alert("请先选择要修改的数据!");
				return;
			};
			row=$("#QueryTable").bootstrapTable('getSelections')[0];
		}
		if (row.isdefault!=null) { $("#txt_isdefault").val(row.isdefault) } else { $("#txt_isdefault").val("")};
		if (row.actionid!=null) { $("#txt_actionid").val(row.actionid) } else { $("#txt_actionid").val("")};
		if (row.sysactionname!=null) { $("#txt_sysactionname").val(row.sysactionname) } else { $("#txt_sysactionname").val("")};
		if (row.sysactionid!=null) { $("#txt_sysactionid").val(row.sysactionid) } else { $("#txt_sysactionid").val("")};
		$("#txt_sysactionid").attr("disabled",true);
		if (row.systemid!=null) { $("#txt_systemid").selectpicker("val",row.systemid) } else { $("#txt_systemid").selectpicker("val","")};
		if (row.params!=null) { $("#txt_params").val(row.params) } else { $("#txt_params").val("")};
		if (row.upid) {
			if (row.upid!=0) {
				expendp($.fn.zTree.getZTreeObj("Tree_up"),$.fn.zTree.getZTreeObj("Tree_up").getNodeByParam("sysactionid",row.upid));
				$.fn.zTree.getZTreeObj("Tree_up").selectNode($.fn.zTree.getZTreeObj("Tree_up").getNodeByParam("sysactionid",row.upid),true);
			};
			$("#txt_upid").val(row.upname);
			$("#txt_upid").data("value",row.upid);
		} else {
			$("#txt_upid").val("");
			$("#txt_upid").data("value","");
		};
		expendp($.fn.zTree.getZTreeObj("Tree_all"),$.fn.zTree.getZTreeObj("Tree_all").getNodeByParam("sysactionid",row.sysactionid));
        $("#myModalLabel").html("修改");
        $("#myModal").modal();
	};
	
	function CommitTable() {
		var commitdata={
			isdefault:$("#txt_isdefault").val(),
			actionid:$("#txt_actionid").val(),
			sysactionname:$("#txt_sysactionname").val(),
			sysactionid:$("#txt_sysactionid").val(),
			systemid:$("#txt_systemid").val(),
			params:$("#txt_params").val(),
			upid:$("#txt_upid").data("value"),
			token:token
		};
		if ($("#myModalLabel").html()=="新增") {
			ajaxarr([{
				url:"sysinfo/sysaction_add",
				title:"新增数据失败！",
				data:commitdata,
				func:function(data) {
					alert("新增数据成功！id："+data.sysactionid);
					QueryTable(data.sysactionid);
					$("#myModal").modal("hide");
				}
			}]);
		} else {
			ajaxarr([{
				url:"sysinfo/sysaction_edit",
				title:"修改数据失败！",
				data:commitdata,
				func:function(data) {
					alert("修改数据成功！");
					QueryTable(commitdata.sysactionid);
					$("#myModal").modal("hide");
				}
			}]);		
		};
	};
	
	function DelTable() {
        var row = $("#QueryTable").bootstrapTable('getSelections')[0];
        if(row == null) {
            alert("请先选择要要显示/隐藏的数据!");
            return;
        };       
		var info=(row.isused==0) ? "显示" : "隐藏" ;
		if(confirm( "您真的确定要"+info+"吗？") == true) {
			ajaxarr([{
				url: "sysinfo/sysaction_del",
				title: info+"数据失败！",
				data:{token:token,sysactionid:row.sysactionid,isused:(row.isused==0) ? 1 : null},
				func: function(data) {
					alert(info+"成功！");
                    QueryTable();
				}
			}]);
		}
	};
	
	function QueryTable(id) {
		$("#QueryTable").bootstrapTable('showLoading');
		ajaxarr([{
				url:"sysinfo/sysaction_query",
				title:"获取系统权限信息失败，请刷新！",
				data:{token:token},
				func: function (data) {
					setting={view: {selectedMulti: false,showIcon: true },
						data: {	key: { name: "sysactionname"}, simpleData: {	enable: true,	idKey: "sysactionid",	pIdKey: "upid",	rootPId: 0}	},
						callback: {
							onDblClick: function(event, treeId, treeNode) {
								$("#txt_sysactionid").val(treeNode.sysactionname);
								$("#txt_sysactionid").data("value",treeNode.sysactionid);
								$("#sysactionModal").modal("hide");
							}
						}
					};
					$.fn.zTree.init($("#Tree_up"),setting , data.info.rows);							
					setting={view: {selectedMulti: false,showIcon: true },
						data: {	key: { name: "sysactionname"}, simpleData: {	enable: true,	idKey: "sysactionid",	pIdKey: "upid",	rootPId: 0}	},
						callback: {
							onClick : function(event, treeId, treeNode, clickFlag) {
								$("#QueryTable").bootstrapTable("resetSearch", treeNode.sysactionid);
							},
							onDblClick: function(event, treeId, treeNode) {
								$("#QueryTable").bootstrapTable('uncheckAll');
								$("#QueryTable").bootstrapTable("checkBy",{field:"sysactionid",values:[treeNode.sysactionid]});
								EditTable();//有点问题
							}
						}
					};
					var zTree=$.fn.zTree.init($("#Tree_all"),setting , data.info.rows);	
					if (id) {expendp(zTree,zTree.getNodeByParam("sysactionid",id));};
					if (data.info.total==0) { $("#QueryTable").bootstrapTable('removeAll'); } 
					else {	
						$("#QueryTable").bootstrapTable('load', data.info.rows); 
						if (id) {
							for (var i=data.info.total;i>0;i--) {
								if (data.info.rows[i-1].typeid==id) {
									$("#QueryTable").bootstrapTable('selectPage', Math.ceil(i/$("#QueryTable").bootstrapTable('getOptions').pageSize));
									break;
								};							
							}
						}
					};
					$("#QueryTable").bootstrapTable('hideLoading');
				}
		}]);		
	};

</script>
<!-- <h4> 选择是否默认:</h4> -->
<div >
	<ul id="Tree_all" class="ztree" ></ul>
</div>
<table id="QueryTable" class='table-striped'></table>
<div id="toolbar" class="btn-group">
	<button id="108" type="button" style="display:none;" class="btn btn-default" onclick="AddTable()"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增</button>
	<button id="109" type="button" style="display:none;" class="btn btn-default" onclick="EditTable()"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改</button>
	<button id="111" type="button" style="display:none;" class="btn btn-default" onclick="DelTable()"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>显示/隐藏</button>
	<button id="112" type="button" style="display:none;" class="btn btn-default" onclick="QueryTable()"><span class="glyphicon glyphico-query" aria-hidden="true"></span>查询</button>
</div>
<div class="modal fade" id="myModal" tabindex="-1" >
	<div class="modal-dialog" >
		<div class="modal-content" >
			<div class="modal-header">
				<h4 class="modal-title" id="myModalLabel"></h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>
			<div class="modal-body form-inline">
				<label for="txt_isdefault" class="col-lg-3 text-center">是否默认</label><input id="txt_isdefault" class="form-control col-lg-3" onkeypress="{ if (event.keyCode==13) {txt_actionid.focus()}}" type="text" placeholder="是否默认"/>
				<label for="txt_actionid" class="col-lg-3 text-center">权限编号</label><input id="txt_actionid" class="form-control col-lg-3" onkeypress="{ if (event.keyCode==13) {txt_sysactionname.focus()}}" type="text" placeholder="权限编号"/>
				<label for="txt_sysactionname" class="col-lg-3 text-center">权限名</label><input id="txt_sysactionname" class="form-control col-lg-3" onkeypress="{ if (event.keyCode==13) {txt_sysactionid.focus()}}" type="text" placeholder="权限名"/>
				<label for="txt_sysactionid" class="col-lg-3 text-center">系统权限编号</label><input id="txt_sysactionid" class="form-control col-lg-3" disabled="disabled" onkeypress="{ if (event.keyCode==13) {txt_systemid.focus()}}" type="text" placeholder="系统权限编号"/>
				<label for="txt_systemid" class="col-lg-3 text-center">系统</label><select id="txt_systemid" class="selectpicker form-control col-lg-3" onkeypress="{ if (event.keyCode==13) {txt_params.focus()}}" title="请选择系统"></select>
				<label for="txt_params" class="col-lg-3 text-center">参数</label><input id="txt_params" class="form-control col-lg-3" onkeypress="{ if (event.keyCode==13) {txt_upid.focus()}}" type="text" placeholder="参数"/>
				<label for="txt_upid" class="col-lg-3 text-center">上级权限</label><input id="txt_upid" class="form-control col-lg-3" onkeypress="{ if (event.keyCode==13) {txt_idcount.focus()}}" data-target="#upModal" data-toggle="modal" type="text" placeholder="上级权限"/>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-primary" id="SaveModal" onclick="CommitTable()"><span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>保存</button>
				<button type="button" class="btn btn-default" data-dismiss="modal" ><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>关闭</button>
			</div>
		</div>
	</div>
</div>
<div class="modal fade" id="upModal" tabindex="-1">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header" >
				<h4 class="modal-title" >选择上级</h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>
			<div class="modal-body">
				<ul id="Tree_up" class="ztree"></ul>
			</div>
			<div class="modal-footer" >
				<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="{$('#txt_upid').val($.fn.zTree.getZTreeObj('Tree_up').getSelectedNodes()[0].sysactionname);$('#txt_upid').data('value',$.fn.zTree.getZTreeObj('Tree_up').getSelectedNodes()[0].sysactionid);}">确认</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>
<div class="modal fade" id="upidModal" tabindex="-1">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header" >
				<h4 class="modal-title" >选择上级权限</h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>
			<div class="modal-body">
				<ul id="Tree_upid" class="ztree" ></ul>
			</div>
			<div class="modal-footer" >
				<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="{$('#txt_upid').val($.fn.zTree.getZTreeObj('Tree_upid').getSelectedNodes()[0].sysactionid);$('#txt_upid').data('value',$.fn.zTree.getZTreeObj('Tree_upid').getSelectedNodes()[0].sysactionid);}">确认</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>